package com.imonsoft.sqm.validator.sys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.imonsoft.common.util.ValidatorUtil;
import com.imonsoft.sqm.pojo.sys.User;
import com.imonsoft.sqm.service.MessageUtil;
import com.imonsoft.sqm.service.sys.UserService;
import com.imonsoft.sqm.vo.sys.UserUntil;

@Component("userValidator")
public class UserValidator implements Validator {

	@Autowired
	private UserService userService;

	@Override
	public boolean supports(Class<?> clazz) {
		return User.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "code", "message.required.user.code", "Code required!");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "message.required.user.name", "Name required!");
		User user = (User) target;
		if (!StringUtils.isEmpty(user.getMail()) && !ValidatorUtil.isEmail(user.getMail())) {
			errors.rejectValue("mail", "message.user.mail.not.right");
		}
		if (!StringUtils.isEmpty(user.getPhone()) && !ValidatorUtil.isTelephone(user.getPhone())) {
			errors.rejectValue("phone", "message.user.phone.not.right");
		}
	}

	public void methodAddValidate(Object target, Errors errors) {
		validate(target, errors);
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "message.required.user.password", "Password required!");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwordConfirm", "message.required.user.password.confirm", "Confirm Password required!");
		UserUntil userVo = (UserUntil) target;
		if (ValidatorUtil.isPasswordLength(userVo.getPassword())) {
			errors.rejectValue("password", "message.user.password.length");
		}
		if (ValidatorUtil.isPassword(userVo.getPassword())) {
			errors.rejectValue("password", "message.user.password.format");
		}
		if (!userVo.getPassword().equals(userVo.getPasswordConfirm())) {
			errors.rejectValue("password", "message.user.password.not.equal");
		}
		List<User> userList = userService.findUserByCode(userVo.getCode());
		if (userList.size() > 0) {
			errors.rejectValue("code", "message.unique.user.code", new Object[] { userVo.getCode() }, "编码不能重复！");
		}
	}

	public void methodModifyValidate(Object target, Errors errors) {
		validate(target, errors);
		User user = (User) target;
		List<User> userList = userService.findUserByCode(user.getCode());
		if (userList.size() == 1 && userList.get(0).getId().intValue() != user.getId().intValue()) {
			errors.rejectValue("code", "message.unique.user.code", new Object[] { user.getCode() }, "编码不能重复！");
		}
	}

	@Autowired
	private MessageUtil messageUtil;

	public List<String> getErrors(BindingResult result) {
		List<String> errors = new ArrayList<String>();
		for (ObjectError error : result.getAllErrors()) {
			errors.add(messageUtil.getMessage(error.getCode(), error.getArguments(), error.getDefaultMessage()));
		}
		return errors;
	}
}
